package stud.aistar.listener;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;

/**
 * Created by Administrator on 2019/10/12.
 */
public class SessionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        if (request.getRequestURI().equals("/treatment/personal/site")) {
            if (request.getSession().getAttribute("user") != null) {
                return true;
            } else {
                StringBuffer url = request.getRequestURL();

                String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(
                        request.getServletContext().getContextPath()
                ).append("/cloginview").toString();
                response.sendRedirect(tempContextUrl);
                return false;
            }
        }




        if (request.getRequestURI().equals("/treatment/reg/in")) {
            if (request.getSession().getAttribute("userinfo") != null) {
                return true;
            } else {
                StringBuffer url = request.getRequestURL();


                String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(
                        request.getServletContext().getContextPath()
                ).append("/personal/site").toString();
                response.sendRedirect(tempContextUrl);
                return false;
            }
        }

        return true;
    }
}
